﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace compusUpdates {
    public partial class FormTray : Form {
        public FormTray() {
            InitializeComponent();

        }

        private void FormTray_Load(object sender, EventArgs e) {
            this.Hide();
            TrayIcon.Visible = true;
            Program.RunStartup();

            //an den exun oristei orise ta
            if (compusUpdates.Properties.Settings.Default.username == "" || compusUpdates.Properties.Settings.Default.password == "") {
                Login loginBox = new Login();
                loginBox.ShowDialog();
            }

            checkForUpdatesStartUp();
            Update_Click(null, null);
            CreateTimer();
        }

        private void Exit_Click(object sender, EventArgs e) {
            Application.Exit();
        }

        private void About_Click(object sender, EventArgs e) {
            AboutBox1 aboutBox = new AboutBox1();
            aboutBox.ShowDialog();
        }

        private void Settings_Click(object sender, EventArgs e) {
            settingsForm SettingsForm = new settingsForm();
            SettingsForm.ShowDialog();
        }

        private void Login_Click(object sender, EventArgs e) {
            Login loginBox = new Login();
            loginBox.ShowDialog();
        }

        private void Update_Click(object sender, EventArgs e) {
            String temps = "";
            TrayIcon.BalloonTipTitle = "Compus Updates";
            TrayIcon.BalloonTipText = "Checking...";
            TrayIcon.ShowBalloonTip(30);

            Program.Lessons.Clear();
            Program.findLessons(Program.login());
            
            foreach (Lesson temp in Program.Lessons) {
                temp.getUpdates();
                temps += temp.printUpdates();
            }
            if (temps.Length > 255) {
                temps = temps.Substring(0, 252) + "...";
            }else if (temps.Length < 5) {
                if (Program.Connected) {
                    temps = "No Updates available";
                } else {
                    if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {
                        temps = "Cannot Connect...\nWrong Credentials?";
                    } else {
                        temps = "Cannot Connect...\nNo Internet Connection!";
                    }
                }//Login loginBox = new Login();
                //loginBox.ShowDialog();
            }
            
            TrayIcon.BalloonTipText = temps;
            
            TrayIcon.BalloonTipTitle = "Compus Updates";

            TrayIcon.BalloonTipClicked += new EventHandler(this.Ballon_Click);
            TrayIcon.ShowBalloonTip(30);

        }

        private void Ballon_Click(object sender, EventArgs e) {
            Process.Start("http://compus.uom.gr/");
        }

        //Settings.Default.refresh*60000
        private void CreateTimer() {
            System.Timers.Timer Timer1 = new System.Timers.Timer();
            Timer1.Enabled = true;
            Timer1.Interval = compusUpdates.Properties.Settings.Default.refresh * 60000;
            Timer1.Elapsed += new System.Timers.ElapsedEventHandler(Timer1_Elapsed);
        }

        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
            Update_Click(null, null);
        }

        private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e) {

            TrayIcon.BalloonTipTitle = "Compus Updates";
            TrayIcon.BalloonTipText = "Checking Version...";
            TrayIcon.ShowBalloonTip(30);

            WebResponse response = null;
            StreamReader reader = null;
            string version = null;

            try {
                if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.ic3man.gr/compus/version.txt");
                    request.Method = "GET";
                    response = request.GetResponse();
                    reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    version = reader.ReadToEnd();

                    response.Close();
                    reader.Close();

                    if (Regex.IsMatch(version, "1.0.8")) {
                        TrayIcon.BalloonTipTitle = "Compus Updates";
                        TrayIcon.BalloonTipText = "You have the latest version!";
                        TrayIcon.ShowBalloonTip(30);
                    } else if (version != null || version != "") {
                        TrayIcon.BalloonTipTitle = "Compus Updates";
                        TrayIcon.BalloonTipText = "New Version Availiable... Check the Website!";
                        TrayIcon.BalloonTipClicked += new EventHandler(this.Ballon_ClickWeb);
                        TrayIcon.ShowBalloonTip(30);
                    }
                }
            } catch (Exception ex) {
                TrayIcon.BalloonTipTitle = "Compus Updates";
                TrayIcon.BalloonTipText = "Cannot Connect to Update Server... Try again later...";
                TrayIcon.BalloonTipClicked += new EventHandler(this.Ballon_ClickWeb);
                TrayIcon.ShowBalloonTip(30);
            }




        }

        private void checkForUpdatesStartUp() {
            WebResponse response = null;
            StreamReader reader = null;
            string version = null;

            try {
                if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.ic3man.gr/compus/version.txt");
                    request.Method = "GET";
                    response = request.GetResponse();
                    reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    version = reader.ReadToEnd();



                    if (!(Regex.IsMatch(version, "1.0.8")) && (version != null || version != "")) {
                        TrayIcon.BalloonTipTitle = "Compus Updates";
                        TrayIcon.BalloonTipText = "New Version Availiable... Check the Website!";
                        TrayIcon.BalloonTipClicked += new EventHandler(this.Ballon_ClickWeb);
                        TrayIcon.ShowBalloonTip(30);
                    }
                }
            } catch (Exception ex) {
            }
        }

        private void Ballon_ClickWeb(object sender, EventArgs e) {
             Process.Start("http://www.ic3man.gr/compus/");
        }

    }
}
